﻿Imports Microsoft.VisualBasic

Public Class images

    Public id_image As Integer
    Public name As String
    Public filename_extension As String


    Public Sub New()
        id_image = 0
        name = ""
        filename_extension = ""
    End Sub



    Public Property name_() As String
        Get
            Return name
        End Get
        Set(ByVal value As String)
            name = value
        End Set
    End Property

    Function insert() As Boolean
        Dim connection_string As String = ConfigurationManager.ConnectionStrings("conn").ConnectionString
        Dim Sqlconnection1 As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection()
        Sqlconnection1.ConnectionString = connection_string
        Sqlconnection1.Open()
        Dim SqlCommand1 As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand()
        Dim string_sql As String = "INSERT INTO images(name,filename_extension) VALUES('" + name + "', '" + filename_extension + "' );"
        SqlCommand1.Connection = Sqlconnection1
        SqlCommand1.CommandText = string_sql
        SqlCommand1.ExecuteNonQuery()
        SqlCommand1.Connection.Close()
        Return True
    End Function

    Function update() As String



        Dim connection_string As String = ConfigurationManager.ConnectionStrings("conn").ConnectionString
        Dim Sqlconnection1 As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection()
        Sqlconnection1.ConnectionString = connection_string
        Sqlconnection1.Open()
        Dim SqlCommand1 As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand()
        Dim string_sql As String = "UPDATE images SET name='" + name + "', filename_extension='" + filename_extension + "' WHERE id_image= " + id_image.ToString


        SqlCommand1.Connection = Sqlconnection1
        SqlCommand1.CommandText = string_sql
        SqlCommand1.ExecuteNonQuery()
        SqlCommand1.Connection.Close()
        Return string_sql
    End Function

    Function first() As String
        Dim sql_first As String = "select top 1 id_image, name, filename_extension from images order by id_image ASC"
        Dim connection_string As String = ConfigurationManager.ConnectionStrings("conn").ConnectionString
        Dim Sqlconnection1 As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection()
        Sqlconnection1.ConnectionString = connection_string
        Sqlconnection1.Open()
        Dim SqlCommand1 As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand()
        SqlCommand1.Connection = Sqlconnection1
        SqlCommand1.CommandText = sql_first
        Dim SqlDataReader1 As System.Data.SqlClient.SqlDataReader
        SqlDataReader1 = SqlCommand1.ExecuteReader()
        If SqlDataReader1.Read() Then
            id_image = SqlDataReader1.GetInt32(0)
            name = SqlDataReader1.GetString(1)
            filename_extension = SqlDataReader1.GetString(2)
        End If
        SqlDataReader1.Close()
        Sqlconnection1.Close()
        Return sql_first


    End Function

    Function select_image_by_id() As String
        Dim sql_next As String = "select id_image, name, filename_extension from images WHERE id_image=" + id_image.ToString()
        Dim connection_string As String = ConfigurationManager.ConnectionStrings("conn").ConnectionString
        Dim Sqlconnection1 As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection()
        Sqlconnection1.ConnectionString = connection_string
        Sqlconnection1.Open()
        Dim SqlCommand1 As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand()
        SqlCommand1.Connection = Sqlconnection1
        SqlCommand1.CommandText = sql_next
        Dim SqlDataReader1 As System.Data.SqlClient.SqlDataReader
        SqlDataReader1 = SqlCommand1.ExecuteReader()
        If SqlDataReader1.Read() Then
            id_image = SqlDataReader1.GetInt32(0)
            name = SqlDataReader1.GetString(1)
            filename_extension = SqlDataReader1.GetString(2)
        End If
        SqlDataReader1.Close()
        Sqlconnection1.Close()
        Return sql_next


    End Function

    Function next_image() As String
        Dim sql_next As String = "select top 1 id_image, name, filename_extension from images WHERE id_image>" + id_image.ToString() + " order by id_image ASC"
        Dim connection_string As String = ConfigurationManager.ConnectionStrings("conn").ConnectionString
        Dim Sqlconnection1 As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection()
        Sqlconnection1.ConnectionString = connection_string
        Sqlconnection1.Open()
        Dim SqlCommand1 As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand()
        SqlCommand1.Connection = Sqlconnection1
        SqlCommand1.CommandText = sql_next
        Dim SqlDataReader1 As System.Data.SqlClient.SqlDataReader
        SqlDataReader1 = SqlCommand1.ExecuteReader()
        If SqlDataReader1.Read() Then
            id_image = SqlDataReader1.GetInt32(0)
            name = SqlDataReader1.GetString(1)
            filename_extension = SqlDataReader1.GetString(2)
        End If
        SqlDataReader1.Close()
        Sqlconnection1.Close()
        Return sql_next


    End Function
    Function previous_image() As String
        Dim sql_previous As String = "select top 1 id_image, name, filename_extension from images WHERE id_image<" + id_image.ToString() + " order by id_image DESC"
        Dim connection_string As String = ConfigurationManager.ConnectionStrings("conn").ConnectionString
        Dim Sqlconnection1 As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection()
        Sqlconnection1.ConnectionString = connection_string
        Sqlconnection1.Open()
        Dim SqlCommand1 As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand()
        SqlCommand1.Connection = Sqlconnection1
        SqlCommand1.CommandText = sql_previous
        Dim SqlDataReader1 As System.Data.SqlClient.SqlDataReader
        SqlDataReader1 = SqlCommand1.ExecuteReader()
        If SqlDataReader1.Read() Then
            id_image = SqlDataReader1.GetInt32(0)
            name = SqlDataReader1.GetString(1)
            filename_extension = SqlDataReader1.GetString(2)
        End If
        SqlDataReader1.Close()
        Sqlconnection1.Close()
        Return sql_previous


    End Function
    Function last_image() As String
        Dim sql_last As String = "select top 1 id_image, name, filename_extension from images order by id_image DESC"
        Dim connection_string As String = ConfigurationManager.ConnectionStrings("conn").ConnectionString
        Dim Sqlconnection1 As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection()
        Sqlconnection1.ConnectionString = connection_string
        Sqlconnection1.Open()
        Dim SqlCommand1 As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand()
        SqlCommand1.Connection = Sqlconnection1
        SqlCommand1.CommandText = sql_last
        Dim SqlDataReader1 As System.Data.SqlClient.SqlDataReader
        SqlDataReader1 = SqlCommand1.ExecuteReader()
        If SqlDataReader1.Read() Then
            id_image = SqlDataReader1.GetInt32(0)
            name = SqlDataReader1.GetString(1)
            filename_extension = SqlDataReader1.GetString(2)
        End If
        SqlDataReader1.Close()
        Sqlconnection1.Close()
        Return sql_last

    End Function

    Function delete() As String 'se inseriu bem ou não

        Dim connection_string As String = ConfigurationManager.ConnectionStrings("conn").ConnectionString

        Dim sqlConnection1 As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection()
        sqlConnection1.ConnectionString = connection_string
        sqlConnection1.Open()

        Dim sqlcomand1 As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand()

        Dim String_delete As String = "delete from images where id_image = " + id_image.ToString()

        'associa o comando a executar com a conexão
        sqlcomand1.Connection = sqlConnection1

        'definir o sql que o comando vai executar
        sqlcomand1.CommandText = String_delete

        'Executa do comando na base de dados base de dados
        sqlcomand1.ExecuteNonQuery()

        'Fecha a ligação á base de dados
        sqlConnection1.Close()

        id_image = 0
        name = ""
        filename_extension = ""

        Return String_delete
    End Function
End Class

